//
//  MachineVersionView.swift
//  xgwjinterconnection
//  机器信息页面
//  Created by WangWeiShuo on 2025/8/18.
//

import SwiftUI
import Combine

struct MachineVersionView: View {
    @Environment(\.dismiss) var dismiss
    @StateObject private var socketManager = SocketManager.shared
    @State private var cancellables = Set<AnyCancellable>()
    // 用于存储定时器订阅
    @State private var cancellable: AnyCancellable?
    // 控制定时器状态
    @State private var isRunning: Bool = false
    
    @State private var versionInfo: VehicleVersionBean?
    
    var body: some View {
        VStack(){
            WikaNavigationView(showBackIcon: true, title: NSLocalizedString("vehicle_info", comment: "机器信息"),onBack: {
                dismiss() }).frame(maxHeight: 65)
            
            FunctionVersionView(versionName: NSLocalizedString("version_meter", comment: "仪表型号"), versionValue: versionInfo?.meter ?? "RK3568", backGroundColor: "color_gray_f3f3f3").padding(.top,-25)
            
            FunctionVersionView(versionName: NSLocalizedString("version_displayhw", comment: "显示器硬件版本号"), versionValue: versionInfo?.displayhw ?? "1.0.0", backGroundColor: "color_white")
            
            FunctionVersionView(versionName: NSLocalizedString("version_displaysw", comment: "显示器软件版本号"), versionValue: versionInfo?.displaysw ?? "1.0.0", backGroundColor: "color_gray_f3f3f3")
            
            FunctionVersionView(versionName: NSLocalizedString("version_enginesw", comment: "发动机程序版本号"), versionValue: versionInfo?.enginev ?? "1.0.0", backGroundColor: "color_white")
            
            FunctionVersionView(versionName: NSLocalizedString("version_mediav", comment: "多媒体面板版本号"), versionValue: versionInfo?.mediav ?? "1.0.0", backGroundColor: "color_gray_f3f3f3")
            
            FunctionVersionView(versionName: NSLocalizedString("version_switchv", comment: "开关面板版本号"), versionValue: versionInfo?.switchv ?? "1.0.0", backGroundColor: "color_white")
            
            FunctionVersionView(versionName: NSLocalizedString("version_controllerv", comment: "控制器软件版本号"), versionValue: versionInfo?.controllerv ?? "1.0.0", backGroundColor: "color_gray_f3f3f3")
            
            FunctionVersionView(versionName: NSLocalizedString("version_airv", comment: "空调软件版本号"), versionValue: versionInfo?.airv ?? "1.0.0", backGroundColor: "color_white")
            
            FunctionVersionView(versionName: NSLocalizedString("version_airserial", comment: "空调序列号"), versionValue: versionInfo?.airserial ?? "1.0.0", backGroundColor: "color_gray_f3f3f3")
            
            
            
            Spacer()
        }
        .navigationTitle("")
        .navigationBarHidden(true)
        .enableSwipeBackGesture()
        .onAppear {
            // 订阅用户信息消息
            socketManager.subscribeToVersionInfo()
                .receive(on: DispatchQueue.main)
                .sink {  info in
                    versionInfo = info
                    print("收到数据 = \(versionInfo)")
                }
                .store(in: &cancellables)
            SendMsgUtils.sendGetMessage(socket: socketManager,type: SendMessageType.GET_MSG_VEHICLE_VERSION)
//            startTimer()
        }
        .onDisappear{
//            stopTimer()
        }
    }
    
    // 启动定时器
    private func startTimer() {
        isRunning = true
        // 创建定时器：每隔1秒发送一次信号，在主线程接收
        cancellable = Timer.publish(every: 5, on: .main, in: .common)
            .autoconnect()
            .sink { _ in
                // 每秒执行的任务
                print("定时器执行")
                SendMsgUtils.sendGetMessage(socket: socketManager,type: SendMessageType.GET_MSG_VEHICLE_VERSION)
            }
    }
    
    // 停止定时器
    private func stopTimer() {
        isRunning = false
        cancellable?.cancel() // 取消订阅
        cancellable = nil
    }
}

struct FunctionVersionView: View {
    let versionName: String
    let versionValue: String
    let backGroundColor: String
    
    var body: some View {
        HStack {
            Text(versionName).font(.callout).foregroundColor(.black)
            Spacer()
            Text(versionValue).font(.footnote).foregroundColor(.black)
        }
        .padding()
        .background(Color(backGroundColor))
        .cornerRadius(8)
        .shadow(color: Color.gray.opacity(0.2), radius: 4, x: 0, y: 2)
        .padding(.horizontal, 16)
    }
}

#Preview(){
    MachineVersionView()
}

